/*
 * Copyright (c) 2004, Steven Baldasty <sbaldasty@bitflippin.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * Contributors:
 *    Steven Baldasty <sbaldasty@bitflippin.org>
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

package org.bitflippin.ninjarobots;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

// Sub-panels of setup panel - one for each team.
// Contain team name label, active program field, and two buttons.

public class ProgramPanel extends CustomPanel implements ActionListener  {

	// Private components.
	private JTextField field = new JTextField("", 15);
	private JButton inputButton = new JButton("Input");
	private JButton clearButton = new JButton("Clear");

	// Access to components for IDE and IDE subclass Team.
	// Only may clear after successful parse.
	public JTextField getField()  { return field; }
	public JButton getInputButton()  { return inputButton; }
	public JButton getClearButton()  { return clearButton; }

	// Link to team associated with program.
	private Team team;
	public Team getTeam()  { return team; }

	// Initialize components.
	public ProgramPanel(NinjaRobots f, int n)  {
		super(new BorderLayout(), f);
		inputButton.addActionListener(this);
		clearButton.addActionListener(this);
		field.setEnabled(false);
		clearButton.setEnabled(false);
		team = new Team(frame, this, n);
		add(team.nameLabel(), BorderLayout.WEST);
		JPanel p = new JPanel(new FlowLayout());
		p.add(field);
		p.add(new JLabel("     "));
		p.add(clearButton);
		p.add(inputButton);
		add(p, BorderLayout.EAST);
	}

	// Button driven action dispatcher.
	// Defined by contract with ActionListener.
	public void actionPerformed(ActionEvent e)  {
		Object o = e.getSource();
		if(o == inputButton)
			pressedInput();
		else if(o == clearButton)
			pressedClear();
	}

	// Swap to team program editor.
	private void pressedInput()  {
		frame.setupPanel.setVisible(false);
		team.setVisible(true);
	}

	// Deactiveate last-parsed program.
	// Only available after successful parse.
	private void pressedClear()  {
		team.clear();
		clearButton.setEnabled(false);
	}

}
